using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
namespace MicroMsg.Scene.Image
{
	public class UpLoadImgContext : IContextBase
	{
		private const string TAG = "UpLoadImgContext";
		private const string JPEG_POSTFIX = ".jpg";
		private const int JPEG_NORMAL_QUALITY = 70;
		private const int JPEG_MIN_QUALITY = 50;
		public const int STATUS_INIT = 0;
		public const int STATUS_READY = 1;
		public const int STATUS_SENDING = 2;
		public const int STATUS_ERROR = 4;
		public const int STATUS_COMPLETE = 5;
		private string newBigImgName = "";
		public bool isResend;
		public int mStatus;
		public string talkerName = "";
		public string filename = "";
		public ParamEx mParamEx;
		public Stream origStream;
		private IsolatedStorageFile mCurrentIsolatedStorage;
		private IsolatedStorageFileStream mBigImageFileStream;
		public MemoryStream bigImageMemoryStream;
		public int mMsgType = 3;
		public ChatMsg chatMsgInfo;
		public MsgTrans imgInfo;
		public NetSceneUploadImage senceHandle;
		public bool needToHandle()
		{
			return this.mStatus == 0;
		}
		public bool isRunning()
		{
			if (this.mStatus != 1)
			{
				if (this.mStatus != 2)
				{
					return false;
				}
			}
			return true;
		}
		public bool needToClean()
		{
			return this.mStatus == 5 || this.mStatus == 4;
		}
		public bool fillContextWithOrigStream()
		{
			if (this.origStream == null || this.origStream.get_Length() <= 0L)
			{
				return false;
			}
			if (this.chatMsgInfo != null && !string.IsNullOrEmpty(this.chatMsgInfo.strClientMsgId))
			{
				if (this.imgInfo == null)
				{
					this.imgInfo = new MsgTrans();
					this.imgInfo.nCreateTime = (long)(Util.getNowMilliseconds() / 1000.0);
					this.imgInfo.strToUserName = this.talkerName;
					this.imgInfo.nStatus = 2;
					this.imgInfo.nTransType = 1;
					this.imgInfo.strClientMsgId = this.chatMsgInfo.strClientMsgId;
					if (!this.saveImageOnLocal(this.filename, this.origStream))
					{
						Log.smethod_1("UpLoadImgContext", "saveImageOnLocal fail!");
						return false;
					}
					if (this.imgInfo.nTotalDataLen <= 0)
					{
						Log.smethod_1("UpLoadImgContext", "saveImageOnLocal fail len =" + this.imgInfo.nTotalDataLen);
						return false;
					}
					if (this.chatMsgInfo == null)
					{
						this.chatMsgInfo = new ChatMsg();
					}
					this.chatMsgInfo.strTalker = this.talkerName;
					this.chatMsgInfo.nMsgType = this.mMsgType;
					this.chatMsgInfo.nStatus = 0;
					this.chatMsgInfo.nIsSender = 1;
					this.chatMsgInfo.strThumbnail = this.imgInfo.strThumbnail;
					this.chatMsgInfo.strPath = this.imgInfo.strImagePath;
					StorageMgr.chatMsg.updateMsg(this.chatMsgInfo);
					this.imgInfo.nMsgLocalID = this.chatMsgInfo.nMsgLocalID;
					StorageMgr.msgImg.method_1(this.imgInfo);
				}
				return true;
			}
			return false;
		}
		private byte[] readFromStream(IsolatedStorageFileStream fileStream, int startPos, int dataLen)
		{
			byte[] result;
			try
			{
				if (fileStream != null && (long)dataLen <= fileStream.get_Length())
				{
					byte[] array = new byte[dataLen];
					fileStream.Seek((long)startPos, 0);
					int num = fileStream.Read(array, 0, dataLen);
					if (num <= 0)
					{
						Log.smethod_1("UpLoadImgContext", "read from stream lenght = " + num);
						result = null;
					}
					else
					{
						result = array;
					}
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("UpLoadImgContext", ex.get_Message());
				result = null;
			}
			return result;
		}
		public bool saveImageOnLocal(string filename, Stream origImgStream)
		{
			if (origImgStream != null && origImgStream.get_Length() > 0L)
			{
				string text = ".jpg";
				byte[] array = new byte[origImgStream.get_Length()];
				int num = origImgStream.Read(array, 0, (int)origImgStream.get_Length());
				ImageFromat imageFromat = ImageFromat.jpeg;
				if (num > 0)
				{
					imageFromat = ImageUtil.getImageFormat(array);
				}
				if (imageFromat == ImageFromat.const_3)
				{
					text = "bmp";
				}
				else
				{
					if (imageFromat == ImageFromat.const_4)
					{
						text = "gif";
					}
					else
					{
						if (imageFromat == ImageFromat.const_2)
						{
							text = "png";
						}
					}
				}
				bool result;
				try
				{
					using (this.mCurrentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
					{
						if (!this.mCurrentIsolatedStorage.DirectoryExists(StorageIO.getImagePath()))
						{
							this.mCurrentIsolatedStorage.CreateDirectory(StorageIO.getImagePath());
						}
						if (filename == null)
						{
							Log.smethod_1("UpLoadImgContext", "origImgName == null ");
							result = false;
						}
						else
						{
							this.newBigImgName = string.Concat(new string[]
							{
								StorageIO.getImagePath(),
								"/",
								filename,
								"_send",
								text
							});
							if (this.mCurrentIsolatedStorage.FileExists(this.newBigImgName))
							{
								this.mCurrentIsolatedStorage.DeleteFile(this.newBigImgName);
							}
							this.mBigImageFileStream = this.mCurrentIsolatedStorage.CreateFile(this.newBigImgName);
							if (this.mBigImageFileStream == null)
							{
								Log.smethod_1("UpLoadImgContext", "Can not Create big Image File");
								result = false;
							}
							else
							{
								this.imgInfo.strImagePath = this.newBigImgName;
								Log.smethod_4("UpLoadImgContext", "Big Img file open complete file name = " + this.newBigImgName);
								if (this.mParamEx != null)
								{
									if (!this.mParamEx.isNeedScale)
									{
										this.saveImage(origImgStream, this.mBigImageFileStream, false, false);
									}
									else
									{
										this.saveImage(origImgStream, this.mBigImageFileStream, false, true);
									}
								}
								else
								{
									this.saveImage(origImgStream, this.mBigImageFileStream, false, true);
								}
								if (this.copyImageStreamToMem())
								{
									this.mBigImageFileStream.Close();
									this.mBigImageFileStream = null;
								}
								this.imgInfo.nTotalDataLen = (int)this.bigImageMemoryStream.get_Length();
								this.imgInfo.nTransDataLen = 0;
								this.imgInfo.nTransType = 1;
								if (this.mParamEx != null && !string.IsNullOrEmpty(this.mParamEx.thumbPath))
								{
									this.imgInfo.strThumbnail = this.mParamEx.thumbPath;
								}
								if (string.IsNullOrEmpty(this.imgInfo.strThumbnail))
								{
									string text2 = MD5Core.GetHashString(filename + Util.getNowMilliseconds());
									text2 = StorageIO.getThumbnailPath() + "/" + text2 + ".jpg";
									if (this.mCurrentIsolatedStorage.FileExists(text2))
									{
										this.mCurrentIsolatedStorage.DeleteFile(text2);
									}
									IsolatedStorageFileStream isolatedStorageFileStream = this.mCurrentIsolatedStorage.CreateFile(text2);
									this.imgInfo.strThumbnail = text2;
									if (isolatedStorageFileStream == null)
									{
										Log.smethod_1("UpLoadImgContext", "Can not Create Image thumb file");
										result = false;
										return result;
									}
									this.saveImage(origImgStream, isolatedStorageFileStream, true, true);
									isolatedStorageFileStream.Close();
									isolatedStorageFileStream.Dispose();
								}
								this.imgInfo.nStatus = 2;
								result = true;
							}
						}
					}
				}
				catch (Exception ex)
				{
					Log.smethod_1("UpLoadImgContext", ex.get_Message());
					result = false;
				}
				return result;
			}
			Log.smethod_1("UpLoadImgContext", "Image's Length = 0 ");
			return false;
		}
		private bool copyImageStreamToMem()
		{
			byte[] array = this.readFromStream(this.mBigImageFileStream, 0, (int)this.mBigImageFileStream.get_Length());
			this.bigImageMemoryStream = new MemoryStream(array);
			if (this.bigImageMemoryStream == null)
			{
				Log.smethod_1("UpLoadImgContext", "bigImageMemoryStream null return");
				return false;
			}
			return true;
		}
		public bool saveImage(Stream origStream, Stream targStream, bool isCreateThumb, bool isNeedScale = true)
		{
			if (origStream != null && targStream != null)
			{
				try
				{
					BitmapImage bitmapImage = new BitmapImage();
					bitmapImage.SetSource(origStream);
					WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
					if (!isCreateThumb)
					{
						if (!isNeedScale)
						{
							Extensions.SaveJpeg(writeableBitmap, targStream, writeableBitmap.get_PixelWidth(), writeableBitmap.get_PixelHeight(), 0, 100);
							bool result = true;
							return result;
						}
						if (bitmapImage.get_PixelHeight() <= 960 && bitmapImage.get_PixelWidth() <= 960 && origStream.get_Length() <= 56320L)
						{
							Extensions.SaveJpeg(writeableBitmap, targStream, writeableBitmap.get_PixelWidth(), writeableBitmap.get_PixelHeight(), 0, 70);
						}
						else
						{
							if (bitmapImage.get_PixelWidth() <= 960 && bitmapImage.get_PixelHeight() <= 960)
							{
								int num = 70;
								if (origStream.get_Length() >= 153600L)
								{
									num = 30;
								}
								else
								{
									if (origStream.get_Length() >= 133120L)
									{
										num = 40;
									}
									else
									{
										if (origStream.get_Length() >= 102400L)
										{
											num = 50;
										}
									}
								}
								Extensions.SaveJpeg(writeableBitmap, targStream, writeableBitmap.get_PixelWidth(), writeableBitmap.get_PixelHeight(), 0, num);
							}
							else
							{
								double num2;
								if (bitmapImage.get_PixelHeight() > bitmapImage.get_PixelWidth())
								{
									num2 = (double)bitmapImage.get_PixelHeight() * 0.0010416666666666667;
								}
								else
								{
									num2 = (double)bitmapImage.get_PixelWidth() * 0.0010416666666666667;
								}
								double num3 = (double)writeableBitmap.get_PixelWidth() / num2;
								double num4 = (double)writeableBitmap.get_PixelHeight() / num2;
								int num5 = (int)num3;
								int num6 = (int)num4;
								Log.smethod_4("UpLoadImgContext", "iScale = " + num2);
								Extensions.SaveJpeg(writeableBitmap, targStream, num5, num6, 0, 70);
								if (targStream.get_Length() > 56320L)
								{
									targStream.Close();
									if (this.mCurrentIsolatedStorage.FileExists(this.newBigImgName))
									{
										this.mCurrentIsolatedStorage.DeleteFile(this.newBigImgName);
									}
									this.mBigImageFileStream = this.mCurrentIsolatedStorage.CreateFile(this.newBigImgName);
									Extensions.SaveJpeg(writeableBitmap, this.mBigImageFileStream, num5, num6, 0, 50);
								}
							}
						}
					}
					else
					{
						if (bitmapImage.get_PixelHeight() <= 100 && bitmapImage.get_PixelWidth() <= 100)
						{
							Extensions.SaveJpeg(writeableBitmap, targStream, writeableBitmap.get_PixelWidth(), writeableBitmap.get_PixelHeight(), 0, 70);
						}
						else
						{
							double num7;
							if (bitmapImage.get_PixelHeight() > bitmapImage.get_PixelWidth())
							{
								num7 = (double)bitmapImage.get_PixelHeight() * 0.01;
							}
							else
							{
								num7 = (double)bitmapImage.get_PixelWidth() * 0.01;
							}
							double num8 = (double)writeableBitmap.get_PixelWidth() / num7;
							double num9 = (double)writeableBitmap.get_PixelHeight() / num7;
							int num10 = (int)num8;
							int num11 = (int)num9;
							Extensions.SaveJpeg(writeableBitmap, targStream, num10, num11, 0, 70);
						}
					}
				}
				catch (Exception ex)
				{
					Log.smethod_1("UpLoadImgContext", ex.get_Message());
					bool result = false;
					return result;
				}
				return true;
			}
			return false;
		}
		public bool updateUploadImgContext(bool isUpdatechatMsg)
		{
			if (this.imgInfo == null)
			{
				return false;
			}
			MsgUIStatus status = MsgUIStatus.Processing;
			if (this.mStatus == 4)
			{
				status = MsgUIStatus.Fail;
			}
			else
			{
				if (this.mStatus == 5)
				{
					status = MsgUIStatus.Success;
				}
			}
			if (this.mStatus == 5)
			{
				if (isUpdatechatMsg)
				{
					this.updateChatMsgState((int)status);
				}
				StorageMgr.msgImg.delByTransID(this.imgInfo.nMsgTransID);
			}
			else
			{
				if (isUpdatechatMsg)
				{
					this.updateChatMsgState((int)status);
				}
				MsgTrans byMsgTransID = StorageMgr.msgImg.getByMsgTransID(this.imgInfo.nMsgTransID);
				if (byMsgTransID != null)
				{
					if (byMsgTransID.nTotalDataLen == byMsgTransID.nTransDataLen)
					{
						if (isUpdatechatMsg)
						{
							this.updateChatMsgState(2);
						}
						StorageMgr.msgImg.delByTransID(this.imgInfo.nMsgTransID);
						return true;
					}
					this.imgInfo.nStatus = this.mStatus;
					this.imgInfo.nLastModifyTime = (uint)(Util.getNowMilliseconds() / 1000.0);
					StorageMgr.msgImg.update(this.imgInfo);
				}
			}
			return true;
		}
		public void updateChatMsgState(int status)
		{
			if (this.chatMsgInfo == null)
			{
				this.chatMsgInfo = StorageMgr.chatMsg.getMsg(this.imgInfo.nMsgLocalID);
				if (this.chatMsgInfo != null)
				{
					this.chatMsgInfo.nStatus = status;
					StorageMgr.chatMsg.modifyMsg(this.chatMsgInfo);
					return;
				}
			}
			else
			{
				this.chatMsgInfo.nStatus = status;
				StorageMgr.chatMsg.updateMsg(this.chatMsgInfo);
			}
		}
		public void loadUploadImgContext(int nImgMsgTranId)
		{
			this.isResend = true;
			if (nImgMsgTranId > 0)
			{
				MsgTrans byMsgTransID = StorageMgr.msgImg.getByMsgTransID(nImgMsgTranId);
				if (byMsgTransID != null)
				{
					if (byMsgTransID.nTransDataLen == byMsgTransID.nTotalDataLen)
					{
						this.updateChatMsgState(2);
						StorageMgr.msgImg.delByTransID(byMsgTransID.nMsgTransID);
						return;
					}
					this.imgInfo = byMsgTransID;
					this.chatMsgInfo = StorageMgr.chatMsg.getMsg(this.imgInfo.nMsgLocalID);
					if (this.chatMsgInfo == null || string.IsNullOrEmpty(this.imgInfo.strImagePath) || string.IsNullOrEmpty(this.imgInfo.strClientMsgId))
					{
						return;
					}
					this.mMsgType = this.chatMsgInfo.nMsgType;
					try
					{
						using (this.mCurrentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
						{
							if (this.mCurrentIsolatedStorage.FileExists(this.imgInfo.strImagePath))
							{
								IsolatedStorageFileStream isolatedStorageFileStream = this.mCurrentIsolatedStorage.OpenFile(this.imgInfo.strImagePath, 3, 1);
								if (this.imgInfo.nTransDataLen < this.imgInfo.nTotalDataLen)
								{
									Log.smethod_4("UpLoadImgContext", "Resend image exists file = " + this.imgInfo.strImagePath);
									this.mBigImageFileStream = isolatedStorageFileStream;
									if (this.copyImageStreamToMem())
									{
										this.mBigImageFileStream.Close();
										this.mBigImageFileStream = null;
									}
									this.chatMsgInfo.nStatus = 0;
									byMsgTransID.nStatus = 2;
									StorageMgr.msgImg.update(byMsgTransID);
									StorageMgr.chatMsg.updateMsg(this.chatMsgInfo);
								}
								else
								{
									this.mBigImageFileStream = null;
								}
							}
						}
						return;
					}
					catch (Exception ex)
					{
						Log.smethod_1("UpLoadImgContext", ex.get_Message());
						return;
					}
				}
				this.updateChatMsgState(2);
				return;
			}
		}
	}
}
